package cn.rayland.library.utils;


import cn.rayland.library.bean.GcodeTask;
import android.content.Context;

public class MachineController {
	private static MachineController instance;
	private MachineManager machineManager;
	private GcodeTask task;
	
	public static MachineController getInstance(Context context){
		if(instance == null){
			instance = new MachineController(context);
		}
		return instance;
	}
	
	private MachineController(){}
	private MachineController(Context context){
		this.machineManager = MachineManager.getInstance(context);
		this.task = new GcodeTask();
	}

	public void xLeft() {
		String x_left = "G130 X"+machineManager.machine.axis_x.driving_voltage+" Y"+machineManager.machine.axis_y.driving_voltage+" Z"+machineManager.machine.axis_z.driving_voltage+" A"+machineManager.machine.extruder_a.driving_voltage+" B"+machineManager.machine.extruder_b.driving_voltage+"\n"+
						"G1 X-100 F1000";
		task.setTaskName("x_left");
		task.setContent(x_left);
		machineManager.sendTask(task, true);
	}

	public void xRight() {
		String x_right = "G130 X"+machineManager.machine.axis_x.driving_voltage+" Y"+machineManager.machine.axis_y.driving_voltage+" Z"+machineManager.machine.axis_z.driving_voltage+" A"+machineManager.machine.extruder_a.driving_voltage+" B"+machineManager.machine.extruder_b.driving_voltage+"\n"+
				"G1 X100 F1000";
		task.setTaskName("x_right");
		task.setContent(x_right);
		machineManager.sendTask(task, true);
	}

	public void yForward() {
		String y_forward = "G130 X"+machineManager.machine.axis_x.driving_voltage+" Y"+machineManager.machine.axis_y.driving_voltage+" Z"+machineManager.machine.axis_z.driving_voltage+" A"+machineManager.machine.extruder_a.driving_voltage+" B"+machineManager.machine.extruder_b.driving_voltage+"\n"+
				"G1 Y100 F1000";
		task.setTaskName("y_forward");
		task.setContent(y_forward);
		machineManager.sendTask(task, true);
	}

	public void yBackForward() {
		String y_backward = "G130 X"+machineManager.machine.axis_x.driving_voltage+" Y"+machineManager.machine.axis_y.driving_voltage+" Z"+machineManager.machine.axis_z.driving_voltage+" A"+machineManager.machine.extruder_a.driving_voltage+" B"+machineManager.machine.extruder_b.driving_voltage+"\n"+
				"G1 Y-100 F1000";
		task.setTaskName("y_backward");
		task.setContent(y_backward);
		machineManager.sendTask(task, true);
	}

	public void zUp() {
		String z_up = "G130 X"+machineManager.machine.axis_x.driving_voltage+" Y"+machineManager.machine.axis_y.driving_voltage+" Z"+machineManager.machine.axis_z.driving_voltage+" A"+machineManager.machine.extruder_a.driving_voltage+" B"+machineManager.machine.extruder_b.driving_voltage+"\n"+
				"G1 Z-100 F500";
		task.setTaskName("z_up");
		task.setContent(z_up);
		machineManager.sendTask(task, true);
	}

	public void zDown() {
		String z_down = "G130 X"+machineManager.machine.axis_x.driving_voltage+" Y"+machineManager.machine.axis_y.driving_voltage+" Z"+machineManager.machine.axis_z.driving_voltage+" A"+machineManager.machine.extruder_a.driving_voltage+" B"+machineManager.machine.extruder_b.driving_voltage+"\n"+
				"G1 Z100 F500";
		task.setTaskName("z_down");
		task.setContent(z_down);
		machineManager.sendTask(task, true);
	}

	public void inFilament() {
		String in_filament = "M109 S"+machineManager.machine.printTemperature+"\n"+
				"G130 X"+machineManager.machine.axis_x.driving_voltage+" Y"+machineManager.machine.axis_y.driving_voltage+" Z"+machineManager.machine.axis_z.driving_voltage+" A"+machineManager.machine.extruder_a.driving_voltage+" B"+machineManager.machine.extruder_b.driving_voltage+"\n"+
				"G92 A0\nG1 A6000 F60";
		task.setTaskName("in_filament");
		task.setContent(in_filament);
		machineManager.sendTask(task, true);
	}

	public void outFilament() {
		String out_filament = "M109 S"+machineManager.machine.printTemperature+"\n"+
				"G130 X"+machineManager.machine.axis_x.driving_voltage+" Y"+machineManager.machine.axis_y.driving_voltage+" Z"+machineManager.machine.axis_z.driving_voltage+" A"+machineManager.machine.extruder_a.driving_voltage+" B"+machineManager.machine.extruder_b.driving_voltage+"\n"+
				"G92 A0\nG1 A-6000 F1000";
		task.setTaskName("out_filament");
		task.setContent(out_filament);
		machineManager.sendTask(task, true);
	}
	
	public void startAdjustHorizon(){
		String startCode = "G130 X"
				+ machineManager.machine.axis_x.driving_voltage + " Y"
				+ machineManager.machine.axis_y.driving_voltage + " Z"
				+ machineManager.machine.axis_z.driving_voltage + " A"
				+ machineManager.machine.extruder_a.driving_voltage + " B"
				+ machineManager.machine.extruder_b.driving_voltage + "\n"
				+ "G1 Z5 F1000\nG161 X F1000\n" + "G161 Y F1000\n"
				+ "G161 Z F500\n" + "G92 X"
				+ (-machineManager.machine.axis_x.length / 2) + " Y"
				+ (-machineManager.machine.axis_y.length / 2) + " Z0 A0 B0\n";
		task.setTaskName("startCode");
		task.setContent(startCode);
		machineManager.sendTask(task, true);
	}
	
	public void leftDown() {
		String leftDown = "G1 Z5 F1000\nG1 X"
				+ (-machineManager.machine.axis_x.length / 2) + " Y"
				+ (-machineManager.machine.axis_y.length / 2) + "\nG161 Z F500";
		task.setTaskName("leftDown");
		task.setContent(leftDown);
		machineManager.sendTask(task, false);
	}

	public void rightDown() {
		String rightDown = "G1 Z5 F1000\nG1 X"
				+ (machineManager.machine.axis_x.length / 2) + " Y"
				+ (-machineManager.machine.axis_y.length / 2) + "\nG161 Z F500";
		task.setTaskName("rightDown");
		task.setContent(rightDown);
		machineManager.sendTask(task, false);
	}

	public void leftUp() {
		String leftUp = "G1 Z5 F1000\nG1 X"
				+ (-machineManager.machine.axis_x.length / 2) + " Y"
				+ (machineManager.machine.axis_y.length / 2) + "\nG161 Z F500";
		task.setTaskName("leftUp");
		task.setContent(leftUp);
		machineManager.sendTask(task, false);
	}

	public void rightUp() {
		String rightUp = "G1 Z5 F1000\nG1 X"
				+ (machineManager.machine.axis_x.length / 2) + " Y"
				+ (machineManager.machine.axis_y.length / 2) + "\nG161 Z F500";
		task.setTaskName("rightUp");
		task.setContent(rightUp);
		machineManager.sendTask(task, false);
	}

	public void center() {
		String center = "G1 Z5 F1000\nG1 X0 Y0 F2000\nG161 Z F500";
		task.setTaskName("center");
		task.setContent(center);
		machineManager.sendTask(task, false);
	}

	public void stop() {
		machineManager.cancel(true);
	}
}
